<?php

// @formatter:off
/**
 * @file t.stats.search.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: t.stats.search.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/tools/t.stats.search.php $
 */
$wcToolScripts["stats.search"] = array(
	"name" => "Search Stats",
	"description" => "Prompts search engine's statistics",
	"class" => "WCToolStatsSearch",
	"url" => "http://code.google.com/p/wcomix/",
	"enabled" => true
);

// @formatter:on

class WCToolStatsSearch extends WCTool {
	//
	// Public methods.
	public function run($spacer = "") {
		global $wcOptions;

		$this->showStatsWords($spacer);
		$this->showStatsCached($spacer);
		$this->showStatsByType($spacer);
	}
	public function showHelp($spacer = "") {
		
	}
	//
	// Protected methods.
	protected function loadOptions() {
		$this->_optionsLoaded = true;
	}
	protected function showStatsByType($spacer) {
		//
		// Creating the statement to clean items of no words.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "select  swi_type as type,\n";
			$query .= "        count(distinct(swi_id)) as items,\n";
			$query .= "        count(*) as words\n";
			$query .= "from    {$this->_dbprefix}search_index\n";
			$query .= "group by swi_type\n";
			$query .= "order by swi_type asc\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		if($stmt->execute()) {
			echo "{$spacer}Stats by item type:\n";
			foreach($stmt->fetchAll() as $row) {
				printf("{$spacer}\t%s: % 10d items % 10d words\n", $row["type"], $row["items"], $row["words"]);
			}
		}
	}
	protected function showStatsCached($spacer) {
		//
		// Creating the statement to clean items of no words.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "select  count(*) as searches\n";
			$query .= "from    {$this->_dbprefix}search_cache\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		if($stmt->execute()) {
			$row = $stmt->fetch();
			printf("{$spacer}Cached Searches:\t% 10d searches\n", $row["searches"]);
		}
	}
	protected function showStatsWords($spacer) {
		//
		// Creating the statement to clean items of no words.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "select  count(*) as words\n";
			$query .= "from    {$this->_dbprefix}search_words\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		if($stmt->execute()) {
			$row = $stmt->fetch();
			printf("{$spacer}Known Words:\t\t% 10d words\n", $row["words"]);
		}
	}
}
?>